<template>
  <div>
    <el-form ref="sRef" :inline="true" :model="jobCl.jobS" class="demo-dynamic">
      <el-form-item label="部门名称" prop="deptName">
        <el-input v-model.string="jobCl.jobS.deptName" autocomplete="off" placeholder="部门名称" type="text" />
      </el-form-item>
      <el-form-item label="岗位名称" prop="name">
        <el-input v-model.string="jobCl.jobS.name" autocomplete="off" placeholder="部门名称" type="text" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchJobSubmit()">搜索</el-button>
        <el-button type="primary" @click="resetS(sRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <el-button type="primary" @click="opendialog()">
          <el-icon>
            <Plus />
          </el-icon>
          添加岗位
        </el-button>
      </el-col>
    </el-row>
  </div>
  <div>
    <el-table :data="jobCl.tableData" stripe style="width: 100%">
      <el-table-column align="center" label="序号" type="index" width="80">
        <template #default="scope">
          {{ (jobCl.jobS.page - 1) * jobCl.jobS.size + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="部门名称" prop="deptName" width="180" />
      <el-table-column align="center" header-align="center" label="岗位名称" prop="name" width="180" />
      <el-table-column align="center" header-align="center" label="岗位编码" prop="code" width="180" />
      <el-table-column align="center" header-align="center" label="创建时间" prop="createTime" width="180" />
      <el-table-column align="center" fixed="right" header-align="center" label="操作">
        <template #default="scope">
          <el-button link size="small" type="primary" @click="edit(scope.row.id)">编辑 </el-button>
          <el-popconfirm title="确定要删除吗?" @confirm="deleteJobs(scope.row.id)">
            <template #reference>
              <el-button link type="primary">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div>
    <el-dialog v-model="jobCl.jobDialog" destroy-on-close title="添加部门" @close="resetDialog(addRef)">
      <el-form ref="addRef" :model="jobCl.jobAdd" :rules="jobCl.rules">
        <el-form-item label="公司" prop="companyId">
          <el-select v-model="jobCl.jobAdd.companyId" placeholder="请选择公司" @change="getDept">
            <el-option v-for="item in jobCl.selectCompanyData" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="部门" prop="deptId">
          <el-cascader v-model="jobCl.jobAdd.deptId" :getCheckedNodes="true" :options="jobCl.selectDeptTree" :props="jobCl.props" clearable />
        </el-form-item>
        <el-form-item label="岗位名称" prop="name">
          <el-input v-model="jobCl.jobAdd.name" type="text"></el-input>
        </el-form-item>
        <el-form-item label="岗位编码" prop="code">
          <el-input v-model="jobCl.jobAdd.code" type="text"></el-input>
        </el-form-item>
        <el-form-item label="顺序" prop="sort">
          <el-input-number v-model="jobCl.jobAdd.sort" :min="1" />
        </el-form-item>
        <el-form-item autocomplete="off" label="简介" prop="describes">
          <Editor v-model:editorValue="jobCl.jobAdd.describes"></Editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveJobs">保存 </el-button>
          <el-button @click="jobCl.jobDialog = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
  <div class="pagination-block">
    <Page :pageData="jobCl.jobS" @selectPage="searchJobSubmit()" />
  </div>
</template>
<script lang="ts" setup>
import { JobClass, JobInfo } from '@/page/types/admin/job'
import { ElMessage, FormInstance } from 'element-plus/es'
import Editor from '@/components/MyEditor.vue'
import { deleteJob, detailJob, jobPage, saveJob, selectCompany, selectDeptBox, updateJob } from '@/api/admin'
import { baseListInfo } from '@/page/types/base/baseList'
import { selectDeptInfo } from '@/page/types/admin/dept'
import Page from '@/components/Page.vue'

const jobCl = reactive(new JobClass())
const sRef = ref()
const addRef = ref()
const searchJobSubmit = () => {
  jobPage(jobCl.jobS).then(res => {
    const data = res.data.records as JobInfo[]
    jobCl.tableData = data
    jobCl.jobS.page = res.data.current
    jobCl.jobS.size = res.data.size
    jobCl.jobS.total = res.data.total
  })
}
const handleSizeChange = (val: number) => {
  jobCl.jobS.size = val
  searchJobSubmit()
}
const handleCurrentChange = (val: number) => {
  jobCl.jobS.page = val
  searchJobSubmit()
}
const resetS = (rest: FormInstance | undefined) => {
  if (!rest) return
  rest.resetFields()
  searchJobSubmit()
}
const resetDialog = (rest: FormInstance | undefined) => {
  if (!rest) return
  rest.resetFields()
  searchJobSubmit()
}
const opendialog = () => {
  jobCl.jobDialog = true
  selectCompany(null).then(res => {
    const data = res.data as baseListInfo[]
    jobCl.selectCompanyData = data
  })
}
const saveJobs = () => {
  if (jobCl.jobAdd.id) {
    updateJob(jobCl.jobAdd).then(res => {
      jobCl.jobDialog = false
      searchJobSubmit()
      ElMessage({
        message: '更新岗位信息成功',
        type: 'success'
      })
    })
  } else {
    saveJob(jobCl.jobAdd).then(res => {
      jobCl.jobDialog = false
      searchJobSubmit()
      ElMessage({
        message: '保存岗位信息成功',
        type: 'success'
      })
    })
  }
}
const edit = (id: string) => {
  jobCl.jobDialog = true
  selectCompany(null).then(res => {
    const data = res.data as baseListInfo[]
    jobCl.selectCompanyData = data
  })
  selectDeptBox({}).then(res => {
    const data = res.data as selectDeptInfo[]
    jobCl.selectDeptTree = data
  })
  detailJob(id).then(res => {
    const data = res.data as JobInfo
    jobCl.jobAdd = data
  })
}
const deleteJobs = (id: string) => {
  deleteJob(id).then(res => {
    searchJobSubmit()
    ElMessage({
      message: '删除岗位信息成功',
      type: 'success'
    })
  })
}
const getDept = (companyId: string) => {
  selectDeptBox({ companyId: companyId }).then(res => {
    const data = res.data as selectDeptInfo[]
    jobCl.selectDeptTree = data
  })
}
onMounted(() => {
  searchJobSubmit()
})
</script>
<style lang="scss" scoped>
.pagination-block {
  position: relative;

  .el-pagination {
    margin-top: 20px;
    position: absolute;
    right: 0%;
  }
}
.el-select {
  width: 200px;
}
</style>
